package me.zhengjie.controller.ui;

import me.zhengjie.annotation.Log;
import me.zhengjie.dto.sqlServer.Processcard;
import me.zhengjie.service.ProcesscardService;
import me.zhengjie.service.dto.ProcesscardQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import me.zhengjie.utils.PageResult;
import me.zhengjie.service.dto.ProcesscardDto;

/**
* @website https://eladmin.vip
* @author zhangYY
* @date 2024-01-12
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "工艺卡管理")
@RequestMapping("/api/processcard")
public class ProcesscardController {

    private final ProcesscardService processcardService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('processcard:list')")
    public void exportProcesscard(HttpServletResponse response, ProcesscardQueryCriteria criteria) throws IOException {
        processcardService.download(processcardService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询工艺卡")
    @ApiOperation("查询工艺卡")
    @PreAuthorize("@el.check('processcard:list')")
    public ResponseEntity<PageResult<ProcesscardDto>> queryProcesscard(ProcesscardQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(processcardService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增工艺卡")
    @ApiOperation("新增工艺卡")
    @PreAuthorize("@el.check('processcard:add')")
    public ResponseEntity<Object> createProcesscard(@Validated @RequestBody Processcard resources){
        processcardService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改工艺卡")
    @ApiOperation("修改工艺卡")
    @PreAuthorize("@el.check('processcard:edit')")
    public ResponseEntity<Object> updateProcesscard(@Validated @RequestBody Processcard resources){
        processcardService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除工艺卡")
    @ApiOperation("删除工艺卡")
    @PreAuthorize("@el.check('processcard:del')")
    public ResponseEntity<Object> deleteProcesscard(@RequestBody Integer[] ids) {
        processcardService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}